package com.blocks.network;

import java.io.IOException;



import com.blocks.ScriptBlocksApplication;
import com.blocks.network.BlockServer.ChatMessage;
import com.jme3.network.Client;
import com.jme3.network.Message;
import com.jme3.network.MessageListener;
import com.jme3.network.Network;

public class BlockClient {
	private Client client;

	public BlockClient(String host) throws IOException {
		client = Network.connectToServer(BlockServer.NAME,
				BlockServer.VERSION, host, BlockServer.PORT,
				BlockServer.UDP_PORT);
		client.addMessageListener(new ChatHandler(), ChatMessage.class);
		client.start();
	}

	private class ChatHandler implements MessageListener<Client> {
		public void messageReceived(Client source, Message m) {
			ChatMessage chat = (ChatMessage) m;

			//System.out.println("Received:" + chat);
			ScriptBlocksApplication.getInstance().setTerminalValue(ScriptBlocksApplication.getInstance().getTerminalValue()+chat);

		}
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}
	
}
